package com.jcli;

import com.jcli.HslCommunication.Core.Types.OperateResult;
import com.jcli.HslCommunication.Core.Types.OperateResultExOne;
import com.jcli.HslCommunication.HslCommunicationDemo.FormLoad;
import com.jcli.HslCommunication.HslCommunicationDemo.FormMelsecA1E;
import com.jcli.HslCommunication.ModBus.ModbusTcpNet;
import com.jcli.HslCommunication.Profinet.Melsec.MelsecA1ENet;

public class Main {

    public static void main(String[] args) {
        // 在此处可以调用各种测试函数来与不同设备进行通信
        // MelsecA1ETest();

        // 创建一个图形用户界面 (GUI) 对象并显示
        // 创建一个名为 formLoad 的 FormLoad 对象
        FormLoad formLoad = new FormLoad();
        // 设置 formLoad 对象为可见状态，即显示窗体界面
        formLoad.setVisible(true);
        // 释放 formLoad 对象，这通常在窗体不再需要时执行以释放资源和关闭窗体
        formLoad.dispose();

        // 创建并显示A-1E二进制窗口
        // FormMelsecA1E form = new FormMelsecA1E();
        // form.setVisible(true);
        // form.dispose();

        /*try {
            // 创建一个 ModbusTcpNet 对象，指定IP地址、端口和设备ID
            ModbusTcpNet modbus = new ModbusTcpNet("127.0.0.1", 503, (byte) 1);

            // 读取指定地址的线圈状态，这里是读取地址为 "100" 的线圈
            OperateResultExOne<Boolean> read = modbus.ReadCoil("100");

            // 打印消息，用于调试
            System.out.println("运行了~~~~");

            // 检查读取操作是否成功
            if (read.IsSuccess) {
                System.out.println("Value:" + read.Content.toString());
            } else {
                System.out.println("Failed:" + read.Message);
            }
        } catch (Exception ex) {
            // 捕获并处理可能的异常
            System.out.println(ex.getMessage());
        }*/

        try {
            // 使当前线程休眠一秒钟，捕获并处理可能的异常
            Thread.sleep(1000);
        } catch (Exception ex) {
            // 捕获并处理可能的异常
        }

    }

    // 这是一个用于测试Melsec A1E系列PLC通信的函数
    private static void MelsecA1ETest() {
        // 创建Melsec A1E客户端对象并与PLC通信
        MelsecA1ENet melsec_net = new MelsecA1ENet("192.168.1.11", 3000);
        melsec_net.SetPersistentConnection();

        // 读取操作示例
        boolean[] M100 = melsec_net.ReadBool("M100", (short) 1).Content; // 读取M100是否通，十进制地址
        boolean[] X20 = melsec_net.ReadBool("X20", (short) 1).Content; // 读取X20是否通，八进制地址
        boolean[] Y20 = melsec_net.ReadBool("Y20", (short) 1).Content; // 读取Y20是否通，八进制地址
        short short_D1000 = melsec_net.ReadInt16("D1000").Content; // 读取D1000的short值
        int int_D1000 = melsec_net.ReadInt32("D1000").Content; // 读取D1000-D1001组成的int数据
        float float_D1000 = melsec_net.ReadFloat("D1000").Content; // 读取D1000-D1001组成的float数据
        long long_D1000 = melsec_net.ReadInt64("D1000").Content; // 读取D1000-D1003组成的long数据
        double double_D1000 = melsec_net.ReadDouble("D1000").Content; // 读取D1000-D1003组成的double数据
        String str_D1000 = melsec_net.ReadString("D1000", (short) 10).Content; // 读取D1000-D1009组成的条码数据

        // 写入操作示例
        melsec_net.Write("M100", new boolean[]{true}); // 写入M100为通
        melsec_net.Write("Y20", new boolean[]{true}); // 写入Y20为通
        melsec_net.Write("X20", new boolean[]{true}); // 写入X20为通
        melsec_net.Write("D1000", (short) 1234); // 写入D1000  short值
        melsec_net.Write("D1000", 1234566); // 写入D1000  int值
        melsec_net.Write("D1000", 123.456f); // 写入D1000  float值
        melsec_net.Write("D1000", 123.456d); // 写入D1000  double值
        melsec_net.Write("D1000", 123456661235123534L); // 写入D1000  long值
        melsec_net.Write("D1000", "K123456789"); // 写入D1000  string值

        // 批量读取示例
        OperateResultExOne<boolean[]> read = melsec_net.ReadBool("M100", (short) 10);
        if (read.IsSuccess) {
            boolean m100 = read.Content[0];
            boolean m101 = read.Content[1];
            boolean m102 = read.Content[2];
            boolean m103 = read.Content[3];
            boolean m104 = read.Content[4];
            boolean m105 = read.Content[5];
            boolean m106 = read.Content[6];
            boolean m107 = read.Content[7];
            boolean m108 = read.Content[8];
            boolean m109 = read.Content[9];
        } else {
            System.out.print("读取失败：" + read.Message);
        }

        // 批量读取字节示例
        OperateResultExOne<byte[]> read1 = melsec_net.Read("D100", (short) 5);
        if (read1.IsSuccess) {
            short D100 = melsec_net.getByteTransform().TransInt16(read1.Content, 0);
            short D101 = melsec_net.getByteTransform().TransInt16(read1.Content, 2);
            short D102 = melsec_net.getByteTransform().TransInt16(read1.Content, 4);
            short D103 = melsec_net.getByteTransform().TransInt16(read1.Content, 6);
            short D104 = melsec_net.getByteTransform().TransInt16(read1.Content, 8);
        } else {
            System.out.print("读取失败：" + read1.Message);
        }

        //解析复杂数据示例
        OperateResultExOne<byte[]> read3 = melsec_net.Read("D4000", (short) 10);
        if (read3.IsSuccess) {
            double 温度 = melsec_net.getByteTransform().TransInt16(read3.Content, 0) / 10d;
            double 压力 = melsec_net.getByteTransform().TransInt16(read3.Content, 2) / 100d;
            boolean IsRun = melsec_net.getByteTransform().TransInt16(read3.Content, 4) == 1;
            int 产量 = melsec_net.getByteTransform().TransInt32(read3.Content, 6);
            String 规格 = melsec_net.getByteTransform().TransString(read3.Content, 10, 10, "ascii");
        } else {
            System.out.print("读取失败：" + read3.Message);
        }

        // 写入测试示例，M100-M104 写入测试
        boolean[] values = new boolean[]{true, false, false, true, true};
        OperateResult write = melsec_net.Write("M100", values);
        if (write.IsSuccess) {
            System.out.print("写入成功");
        } else {
            System.out.print("写入失败：" + write.Message);
        }

        // 单个位读取示例
        OperateResultExOne<Boolean> read2 = melsec_net.ReadBool("M100");
        if (read2.IsSuccess) {
            System.out.println(read2.Content);
        } else {
            System.out.println("读取失败：" + read.Message);
        }
    }
}
